import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 28463
 * Date: 2022—07—07
 * Time: 16:01
 */
public class MyStack {
    public int[] elem;
    public int usedSize;

    public static final int DEFAULT_CAPATI = 10;

    public MyStack() {
        elem = new int[DEFAULT_CAPATI];
    }
    //入栈
    public void push(int val) {
        //判断栈满
        if (isFull()) {
            elem = Arrays.copyOf(elem,2*elem.length);
        }
        elem[usedSize] = val;
        usedSize++;
    }
    //判断栈满
    public boolean isFull() {
        if (usedSize == elem.length) {
            return true;
        }
    return false;
    }
    //删除栈顶元素
    public int pop() {
        if (isEmpty()) {
            throw new EmptyStackException("栈是空的");
        }
        int oldVal = elem[usedSize-1];
        usedSize--;
        return oldVal;
    }
    //判断栈空
    public boolean isEmpty() {
        return usedSize == 0;
    }
    //获取栈顶元素 不删除
    public int peek() {
        if (isEmpty()) {
            throw new EmptyStackException("栈是空的");
        }
        return elem[usedSize-1];
    }
    //
    public int getUsedSize() {
        return usedSize;
    }
}
